﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace PuzzleDiamonds
{
    public class ScreenMain : Screen
    {
        private Scene m_screenScene;
        private SceneSprite m_starSprite;
        private TextBlock m_gameTitle;
        private RotateTransform m_rotateTransform;
        private double m_Tick;
        private bool m_bMusicPlaying;
        private SceneExplodeParticleEmitter m_particleEmitter;

        public ScreenMain(int pScreenID, Canvas pScreenCanvas)
            : base(pScreenID, pScreenCanvas)
        {
            m_screenScene = new Scene(m_screenCanvas);

            m_starSprite = new SceneSprite("images/star.png", new Point(360.0, 0.0), new Point(0.0, 0.0), 0.0);
            m_gameTitle = (TextBlock)((Canvas)(pScreenCanvas.Parent)).Children.FindName("mainGameTitle");
            m_particleEmitter = new SceneExplodeParticleEmitter("images/particle.png", new Point(380.0, 400.0), new Point(0.0, 0.0), 0.0, m_screenScene, 50, -1, 20.0, 2.0);

            m_rotateTransform = new RotateTransform();
            m_rotateTransform.Angle = 0.0;
            m_Tick = 0.0;
            m_bMusicPlaying = false;

            m_gameTitle.RenderTransform = m_rotateTransform;

            m_screenScene.AddObject(m_starSprite);
            m_screenScene.AddObject(m_particleEmitter);
        }

        public void OnBackgroundMusicEnd(object sender, EventArgs e)
        {
            MediaElement me = SoundManager.Instance().GetMediaElement("backgroundMusic");

            me.Position = new TimeSpan(0, 0, 0);
            me.Play();
        }

        public override void OnActivate()
        {
            base.OnActivate();

            if (!m_bMusicPlaying)
            {
                MediaElement me = SoundManager.Instance().GetMediaElement("backgroundMusic");
                
                me.Play();
                me.MediaEnded += OnBackgroundMusicEnd;

                m_bMusicPlaying = true;
            }
        }

        public override void OnMouseLeftClick(double pMouseX, double pMouseY)
        {
            base.OnMouseLeftClick(pMouseX, pMouseY);

            ScreenManager.Instance().ActivateScreen((int)ScreenTypes.ctGame);
        }

        public override void Update(double pTick)
        {
            base.Update(pTick);

            m_rotateTransform.Angle = 2.5 * Math.Sin(m_Tick);
            // m_particleEmitter.Position = new Point(380.0 + Math.Sin(m_Tick) * 20.0, 400.0);

            m_Tick += 0.5;

            m_screenScene.Update(pTick);
        }
    }
}
